package com.player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.util.Objects;

public class ImageJpanel extends JPanel {
    private BufferedImage image;

    private long ptsTime;

    private Point lastPoint;

    private Point currentPoint;

    private long timeMillis = System.currentTimeMillis();

    private boolean isShowInfo = true;

    public ImageJpanel() {
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                currentPoint = e.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                currentPoint = e.getPoint();
            }
        });
    }


    private long addTime = 0;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (Objects.nonNull(image)) {
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
        long nowTime = System.currentTimeMillis();
        if (nowTime - timeMillis > 500 + addTime) {
            timeMillis = nowTime;
            isShowInfo = currentPoint == null || !currentPoint.equals(lastPoint);
            lastPoint = currentPoint;
            if (isShowInfo) {
                addTime = 1000 * 5;
            } else {
                addTime = 0;
            }
        }
        if (isShowInfo) {
            g.setColor(Color.pink);
            g.drawString(Data.FILENAME, 20, 50);
            long w = getWidth() * ptsTime / Data.TIMESTAMP;
            g.drawLine(0, getHeight() - 10, (int) w, getHeight() - 10);
        }

    }


    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public void setPtsTime(long ptsTime) {
        this.ptsTime = ptsTime;
    }
}
